(function($) {
    /**
     * 分页条.
     */
    var paginate = $.fn.paginate = function(opts) {
        opts = $.extend({}, $.fn.paginate.defaults, opts);
        return this.each(function() {              
            paginate.refresh($(this), opts);
        });
    };    
    /**
     * 分页条默认配置.
     * 这种方式便于重写默认配置.
     */
    paginate.defaults = {
        count : 5,
        start : 12,
        display : 5,
        border : true,
        border_color : '#fff',
        text_color : '#8cc59d',
        background_color : 'black',
        border_hover_color : '#fff',
        text_hover_color : '#fff',
        background_hover_color : '#fff',
        rotate : true,
        images : true,
        mouse : 'slide',
        onChange : function() {
            return false;
        }
    };

    paginate.refresh = function($paginate, opts){
        $paginate.empty();
        
        var clsSuffix = opts.images ? '-img' : '';                
        var spreviousclass = 'jPag-sprevious' + clsSuffix;       
        var snextclass = 'jPag-snext' + clsSuffix;       
        
        var $firstPage = $('<a>').addClass('jPag-first').html('First');

        if (opts.rotate) {
            var $scrollLeftBtn = $('<span>').addClass(spreviousclass);
            var $scrollRightBtn = $('<span>').addClass(snextclass);
            if (!opts.images){
                $scrollLeftBtn.html('&laquo;');
                $scrollRightBtn.html('&raquo;');
            }            
        }

        var $pageBackCtrl = $('<div>').addClass('jPag-control-back');
        $pageBackCtrl.append($firstPage).append($scrollLeftBtn);

        var $pageNumWrapper = $('<div class="jPag-pages-wrapper">');
        var $pageNumUL = $('<ul>').addClass('jPag-pages');
        
        for (var i = 1, pageCount = opts.count + 1; i < pageCount; i++) {            
            $('<li>').html('<a class="' + ( i == opts.start ? 'jPag-current' : '' ) + '">' + i + '</a>').appendTo($pageNumUL);
        }
        $pageNumWrapper.append($pageNumUL);       

        var $lastPage = $('<a>').addClass('jPag-last').html('Last');
        var $pageForwardCtrl = $('<div>').addClass('jPag-control-front');
        $pageForwardCtrl.append($scrollRightBtn).append($lastPage);

        //append all:
        $paginate.addClass('jPaginate').append($pageBackCtrl).append($pageNumWrapper).append($pageForwardCtrl);

        var pageNum_css = {
            'color' : opts.text_color
        };
        if (opts.border) { 
            pageNum_css['border'] = '1px solid ' + opts.border_color;
        }
        if (opts.background_color){
            pageNum_css['background-color'] = opts.background_color;
        }

        var pageNum_hover_css = {
            'color' : opts.text_hover_color
        }; 
        if (opts.background_hover_color){ 
            pageNum_hover_css['background-color'] = opts.background_hover_color;
        } 
        if (opts.border && opts.border_hover_color) {
            pageNum_hover_css['border'] = '1px solid ' + opts.border_hover_color;
        }
        
        //初始化页码样式.    
        $paginate.find('a').css(pageNum_css);
        $paginate.find('a.jPag-current').css(pageNum_hover_css);
        
        //页码鼠标悬浮事件.
        $paginate.find('a').hover(function() {
            $(this).css(pageNum_hover_css);
        }, function() {
            if(!$(this).hasClass('jPag-current')){
                $(this).css(pageNum_css);
            }
        });
        
        $paginate.css('padding-left', $firstPage.parent().width() + 5 + 'px');
               
        var pageNumULWidth = 0;
        $paginate.find('li').each(function(i) {
            if (i === (opts.display - 1)) {
                $pageNumWrapper.css('width', this.offsetLeft + this.offsetWidth - $pageBackCtrl.width() + 'px');  
            }
            pageNumULWidth += $(this).outerWidth();
        });
        $pageNumUL.css('width', pageNumULWidth + 'px');

        if (opts.rotate) {
            
            var thumbs_scroll_interval;
            $scrollLeftBtn.hover(function() {
                thumbs_scroll_interval = setInterval(function() {
                    var left = $pageNumWrapper.scrollLeft() - 1;
                    $pageNumWrapper.scrollLeft(left);
                }, 20);
            }, function() {
                clearInterval(thumbs_scroll_interval);
            });
            $scrollRightBtn.hover(function() {
                thumbs_scroll_interval = setInterval(function() {
                    var left = $pageNumWrapper.scrollLeft() + 1;
                    $pageNumWrapper.scrollLeft(left);
                }, 20);
            }, function() {
                clearInterval(thumbs_scroll_interval);
            });            
            
            if (opts.mouse == 'press') {
                var thumbs_mouse_interval;
                $scrollRightBtn.mousedown(function() {
                    thumbs_mouse_interval = setInterval(function() {
                        var left = $pageNumWrapper.scrollLeft() + 5;
                        $pageNumWrapper.scrollLeft(left);
                    }, 20);
                }).mouseup(function() {
                    clearInterval(thumbs_mouse_interval);
                });
                $scrollLeftBtn.mousedown(function() {
                    thumbs_mouse_interval = setInterval(function() {
                        var left = $pageNumWrapper.scrollLeft() - 5;
                        $pageNumWrapper.scrollLeft(left);
                    }, 20);
                }).mouseup(function() {
                    clearInterval(thumbs_mouse_interval);
                });
            } else {
                $scrollLeftBtn.click(function(e) {
                    var width = $pageNumWrapper.width() - 10;
                    var left = $pageNumWrapper.scrollLeft() - width;
                    $pageNumWrapper.animate({
                        scrollLeft : left + 'px'
                    });
                });

                $scrollRightBtn.click(function(e) {
                    var width = $pageNumWrapper.width() - 10;
                    var left = $pageNumWrapper.scrollLeft() + width;
                    $pageNumWrapper.animate({
                        scrollLeft : left + 'px'
                    });
                });
            }
        }

        //点击第一页.
        $firstPage.click(function(e) {
            $pageNumWrapper.animate({
                scrollLeft : '0px'
            });
            $pageNumWrapper.find('li').eq(0).click();
        });
        //点击最后一页.
        $lastPage.click(function(e) {
            $pageNumWrapper.animate({
                scrollLeft : $pageNumWrapper.width() + 'px'
            });
            $pageNumWrapper.find('li').eq(opts.count - 1).click();
        });

        //click a page
        $pageNumWrapper.find('li').click(function(e) {
            var $currentPageNum = $(this).find('>a');
            var $originalPageNum = $pageNumUL.find('li > a.jPag-current');            
            $originalPageNum.css(pageNum_css).removeClass("jPag-current");
            $currentPageNum.css(pageNum_hover_css).addClass("jPag-current");
               
            adjustPageNumPos($(this));            
            opts.onChange(parseInt($.trim($currentPageNum.text())));
        });
        
        //调整页码位置.
        function adjustPageNumPos($pageNumLi){
            var left = $pageNumLi.get(0).offsetLeft / 2;           
            var tmp = left - ($pageNumWrapper.width() / 2);
            $pageNumWrapper.animate({
                scrollLeft : left + tmp - $firstPage.parent().width() + 'px'
            });    
        }  
        adjustPageNumPos($pageNumWrapper.find('li').eq(opts.start - 1));      
    };    
})(jQuery);